//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005-2009 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using WinPcap;

namespace SoapTrace
{
	/// <summary>
	/// Displays packets received.
	/// </summary>
	public partial class PacketsReceivedForm : System.Windows.Forms.Form
	{
		private PacketViewer m_Viewer;
		
		/// <summary>
		/// Creates a new instance of the <see cref="PacketsReceivedForm"/>
		/// class.
		/// </summary>
		public PacketsReceivedForm( PacketViewer viewer )
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent( );
			
			m_Viewer = viewer;
		}

		/// <summary>
		/// Adds the log entries to the display.
		/// </summary>
		/// <param name="entries">Collection of packet log entries.</param>
		public void AddLogEntries( PacketLogEntryCollection entries )
		{
			foreach( PacketLogEntry Entry in entries )
			{
				AddLogEntry( Entry );
			}
			
			if( PacketsListView.Items.Count == 0 )
			{
				SaveAsButton.Enabled = false;
			}
		}
		
		/// <summary>
		/// Adds a single log entry to the display.
		/// </summary>
		/// <param name="entry">A packet log entry.</param>
		public void AddLogEntry( PacketLogEntry entry )
		{
			ListViewItem Item = new ListViewItem( entry.PacketReceivedTime.ToString( "HH:mm:ss yyyy-MM-dd", CultureInfo.InvariantCulture ) );
			Item.SubItems.Add( entry.Packet.GetBytes( ).Length.ToString( ) );
			
			TcpPacket TcpPacket = null;
			
			if( entry.PacketType == PacketLogEntry.IP )
			{
				IPPacket IPPacket = new IPPacket( entry.Packet.GetBytes( ) );
				TcpPacket = ( TcpPacket )( IPPacket.InnerPacket );
			}
			else
			{
				TcpPacket = TcpPacket.GetTcpPacket( entry.Packet );
			}
			
			if( TcpPacket != null )
			{
				Item.SubItems.Add( TcpPacket.SourceAddress.ToString( ) );
				Item.SubItems.Add( TcpPacket.DestinationAddress.ToString( ) );
				
				TcpControlTypes ControlType = ( TcpControlTypes )TcpPacket.ControlFlag;
				Item.SubItems.Add( ControlType.ToString( ) );
				
				Item.SubItems.Add( TcpPacket.SequenceNumber.ToString( ) );
				Item.SubItems.Add( TcpPacket.AcknowledgementNumber.ToString( ) );
			}
			
			Item.Tag = entry;
			
			PacketsListView.Items.Add( Item );
		}
		
		/// <summary>
		/// Gets or sets the text associated with the selected packet.
		/// </summary>
		public string SelectedItemText
		{
			get
			{
				return PacketBytesTextBox.Text;
			}
			
			set
			{
				PacketBytesTextBox.Text = value;
			}
		}
		
		/// <summary>
		/// Removes all packets on display.
		/// </summary>
		public void Clear( )
		{
			PacketsListView.Items.Clear( );
			SelectedItemText = String.Empty;
		}
				
		private void SaveAsButtonClick( object sender, EventArgs e )
		{
			m_Viewer.SavePackets( );
		}
		
		private void PacketsListViewSelectedIndexChanged( object sender, EventArgs e )
		{
			if( PacketsListView.SelectedItems.Count > 0 )
			{
				PacketLogEntry Entry = ( PacketLogEntry )( PacketsListView.SelectedItems[ 0 ].Tag );
				m_Viewer.PacketSelected( Entry.Packet );
			}
		}
		
		private void ClearButtonClick( object sender, EventArgs e )
		{
			m_Viewer.Clear( );
		}
		
		private void OpenButtonClick( object sender, EventArgs e )
		{
			m_Viewer.OpenFile( );
		}
	}
}
